home *** CD-ROM | disk | FTP | other *** search
/ Trading on the Edge / Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin / pc / mac_file / vendor_d / neuralwa / nw2v50 / tspio.c < prev    next >
Text File  |  1993-08-23  |  16KB  |  568 lines

  1. /* 5:30 July 7, 1988 TSPIO.c TSP interface routine  */
  2.  
  3. /************************************************************************
  4.  * Copyright(C) 1987-1992 NeuralWare Inc                                *
  5.  * Penn Center West, IV-227, Pittsburgh, PA 15276                       *
  6.  * Telephone: (412) 787-8222    FAX: (412) 787-8220                     *
  7.  *                                                                      *
  8.  * All rights reserved.  No part of this program may be reproduced,     *
  9.  * stored in a retrieval system, or transmitted, in any form or by any  *
  10.  * means, electronic, mechanical, photocopying, recording or otherwise  *
  11.  * without the prior written permission of the copyright owner,         *
  12.  * NeuralWare, Inc.                                                     *
  13.  *                                                                      *
  14.  *                          PROPRIETARY NOTICE                          *
  15.  *                                                                      *
  16.  * This document is the property of NeuralWare, Inc. and contains       *
  17.  * trade-secrets and other proprietary information.  The information    *
  18.  * herein is reserved as proprietary to NeuralWare, and is not to be    *
  19.  * published, reproduced, copied, disclosed, used, or reverse           *
  20.  * engineered without the express written consent of a duly authorized  *
  21.  * representative of NeuralWare.                                        *
  22.  ************************************************************************
  23.  */
  24.  
  25. #include "userutl.h"
  26. #ifndef SUN
  27. #ifndef DLC
  28. #include <stdlib.h>
  29. #endif
  30. #endif
  31. #include <string.h>
  32.  
  33. #include "tsp.h"
  34.  
  35. #ifdef MAC
  36. #include  "macuio.redef"
  37. #endif
  38.  
  39. /************************************************************************
  40.  *              
  41.  *  Traveling Sales Person Problem Interface
  42.  *                 
  43.  ************************************************************************
  44.  */
  45.  
  46. #define X 0
  47. #define Y 1
  48.  
  49. #define CITY_WIDTH  3 /* Half width of a displayed city */
  50.  
  51. static  int city_pos[MAX_NUM_CITIES][2]; /* screen positions of cities */
  52. static  int city_stop[MAX_NUM_CITIES];   /* itinerary index */
  53.  
  54. /* window parameters */
  55. static int  xstrt, ystrt, width = {200}, height, xmargin = {20}, ymargin;
  56.  
  57. #define TSP_STEPSIZE  0
  58. #define TSP_NEXTSTEP  1
  59. #define TSP_QUIT  2
  60.  
  61. char  response[100];
  62. static  CITY  cities[MAX_NUM_CITIES], *city_ptr;
  63. static  int num_cities;
  64.  
  65. /* Local menu definitions */
  66. #define STPSZ_STR "Step size: ***"
  67. #define NXTST_STR "Next step"
  68. #define QUIT_STR  "Quit"
  69.  
  70. static GMENU_ITEM TspList[] = {
  71.     { TSP_STEPSIZE, 0, 0 },
  72.     { TSP_NEXTSTEP, 0, 0 },
  73.     { TSP_QUIT,     0, 0 }
  74. };
  75.  
  76. static GMENU  TspMenu  = {
  77.   0,
  78.   3,        /* 3 items */
  79.   2,        /* key */
  80.   0x0000
  81. };
  82.  
  83. static int  xmwdw,ymwdw;    /* menu window position */
  84. static int  stepsize = {10};  /* how often display is updated */
  85. static int  curr_iteration, prev_iteration;
  86.  
  87. extern  double  sqrt( );
  88.  
  89. #ifdef DLC
  90. /* --- prototypes --- */
  91. double  calc_itin( );
  92.   get_line( FILE * );
  93.   disp_itin( int, int );
  94. #endif
  95.  
  96. get_line(fp)
  97. FILE *fp;
  98. {
  99. #ifdef MAC
  100. #define NEWLN 015
  101. #else
  102. #define NEWLN 012
  103. #endif
  104.  
  105.   int i;
  106.   char c;
  107.   int notempty=0;
  108.   
  109.   for (i=0;i<100;i++)
  110.     {
  111.     if ((response[i] = fgetc(fp)) == EOF)
  112.       return EOF;
  113.     if (response[i] == NEWLN) {
  114.       if ( notempty == 0 ) {
  115.         i = -1;
  116.         continue;
  117.       } else break;
  118.     }
  119.     if (response[i] == '!')     /* comment */
  120.       {
  121.       /* skip to line end*/
  122.       while(((c=fgetc(fp)) != NEWLN) && (c != EOF)) ;
  123.       if (c == EOF)
  124.         return EOF;
  125.       if (i == 0 || notempty == 0)
  126.         { /* if at beginning just skip */
  127.         i = -1;
  128.         continue;
  129.         }
  130.       else
  131.         break;  /* return to calling program */
  132.       }
  133.     /* Must be valid to fall through - flag non-empty line */
  134.     if (response[i] > ' ' && response[i] <= 0x7f ) notempty = 1;
  135.     }
  136.   if (i==100)
  137.     return EOF;
  138.   response[i]='\0';
  139.   return 0;
  140. }
  141.  
  142. disp_itin( citycolor, linecolor )
  143. int citycolor;
  144. int linecolor;
  145. {
  146.   int wx, wy, wz;
  147.   
  148.   /* Draw itinerary */
  149.   for (wx = 0; wx < num_cities - 1; ) {
  150.     wy = city_stop[wx++];
  151.     wz = city_stop[wx];
  152.     ug_line( 1, linecolor, 0,
  153.        city_pos[wy][X], city_pos[wy][Y],
  154.        city_pos[wz][X], city_pos[wz][Y], 0 );
  155.   }
  156.   /* Complete the tour */
  157.   wy = city_stop[num_cities - 1];
  158.   wz = city_stop[0];
  159.   ug_line( 1, linecolor, 0,
  160.      city_pos[wy][X], city_pos[wy][Y],
  161.      city_pos[wz][X], city_pos[wz][Y], 0 );
  162.   
  163.   /* Display cities */
  164.   for ( wx=0; wx < num_cities; wx++ )
  165.     ug_boxf( 1, citycolor, 0,
  166.        city_pos[wx][X]-CITY_WIDTH, city_pos[wx][Y]-CITY_WIDTH,
  167.        city_pos[wx][X]+CITY_WIDTH, city_pos[wx][Y]+CITY_WIDTH);
  168. }
  169.  
  170. double  calc_itin( )    /* Calculate length of itinerary */
  171. {
  172.   int   wx, wy, wz;
  173.   double  dt;   /* distance travelled */
  174.   double  xdiff, ydiff;
  175.   
  176.   dt = 0.0;
  177.   
  178.   for (wx = 0; wx < num_cities; ) {
  179.     wy = city_stop[wx++];
  180.     wz = city_stop[wx%num_cities];
  181.     xdiff = cities[wz].xloc - cities[wy].xloc;
  182.     ydiff = cities[wz].yloc - cities[wy].yloc;
  183.     dt += sqrt( xdiff * xdiff + ydiff * ydiff );
  184.   }
  185.   return( dt );
  186. }
  187.  
  188.     
  189. int nocase_strcmp(str1,str2)
  190. char *str1;
  191. char *str2;
  192. {
  193.   int diff;
  194.   int mask;
  195.   
  196.   mask = ~('a' ^ 'A');
  197.   diff = (*str1 & mask) - (*str2 & mask);
  198.   while((*str1 != '\0') && (*str2 != '\0') && (diff == 0))
  199.     {
  200.     diff = (*str1 & mask) - (*str2 & mask);
  201.     str1++;
  202.     str2++;
  203.     }
  204.   return diff;
  205. }
  206. /*   */
  207. /************************************************************************
  208.  *
  209.  *  UsrIO - user I/O routine to handle requests from NWORKS
  210.  *
  211.  ************************************************************************
  212.  */
  213.  
  214. static int AbortFlag  = 0;
  215. static int InitFlag = 0;    /* initialize things flag */
  216.  
  217. int UsrIO()     /* handle NWORKS requests */
  218. {
  219.     int   wx, wy, wz;       /* work indices */
  220.     int   button;      /* mouse interface */
  221.     int   xsize, ysize, ncolor, chrx, chry; /* graphics parameters */
  222.     GMENU_ITEM  *gmip;
  223.     char  *city_fname;        /* city file name */
  224.     int   nlayp, ninp, noutp, ltype;    /* Network parameters */
  225.     char  *csp;         /* Control strategy pointer*/
  226.     float xlow, xhigh, ylow, yhigh, diff, scl;  /* world city params */
  227.     float scrn_ratio;       /* Aspect ratio of screen */
  228.     float maxout;
  229.     FILE *fp;
  230.     char  sp[60];       /* string pointer */
  231.  
  232.     if ( InitFlag == 0 )  {
  233.       TspList[0].text = STPSZ_STR;
  234.       TspList[1].text = NXTST_STR;
  235.       TspList[2].text = QUIT_STR;
  236.       
  237.       TspMenu.item=TspList;
  238.  
  239.       InitFlag = 1;
  240.     }
  241.  
  242.     IORTNCDE = 0;       /* good return for data */
  243.     switch( IOREQCDE ) {
  244.     case RQ_ATTENTION:
  245.   /* This is invoked at the request of the user from the
  246.      execute menu.  It allows parameters to be changed or
  247.      altered.  Any graphics or other interactions are allowable
  248.      as usual for the other options.
  249.   */
  250.   break;
  251.  
  252.     case RQ_LSTART:       /* starting learn */
  253.   /* This tells the user that the program is about to start
  254.      learning.  It is called once for a LEARN ALL, LEARN ONE,
  255.      LEARN N, or LEARN START
  256.   */
  257.   break;
  258.  
  259.     case RQ_LEARNOUT:       /* read desired output */
  260.   /* IODATA points to an empty array of IOCOUNT values.  The
  261.      elements of the array will become the desired outputs for
  262.      training purposes.  These desired outputs correspond to
  263.      the most recent "RQ_LEARNIN" request.
  264.   */
  265.     case RQ_RCLTST:   /* read desired output during recall test */
  266.   /* IODATA points to an empty array of IOCOUNT values.  The
  267.      elements of the array will become the desired outputs for
  268.      recall purposes.  This request is only made during a
  269.      Execute Network/Recall Test.
  270.   */
  271.   break;
  272.  
  273.     case RQ_LEARNRSLT:
  274.   /* IODATA points to an array of IOCOUNT values.  These are the
  275.      output of the network caused by the inputs from RQ_LEARNIN.
  276.   */
  277.   break;
  278.  
  279.     case RQ_RSTART:       /* starting recall */
  280.   /* This tells the user that the program is about to start
  281.      a recall.  It is called once for a RECALL ALL, RECALL ONE,
  282.      RECALL N, or RECALL START.
  283.   */
  284.       AbortFlag = 0;
  285.       /* Read network parameters */
  286.       ug_rdnetinf( &nlayp, &ninp, &noutp, <ype, &csp, &city_fname );
  287.       /* Get city file information */
  288.       if ((fp=fopen(city_fname,"r")) == NULL) {
  289.         PutStr("Cannot find default city file; please enter file name:");
  290.         city_fname = GetStr();
  291.         PutStr("\n");
  292.  
  293.         if ((fp=fopen(city_fname,"r")) == NULL) {
  294.     PutStr("Bad filename\n");
  295.     IORTNCDE = -1;
  296.     AbortFlag = 1;
  297.     return(0);
  298.   }
  299.       }
  300.   do {
  301.     if (get_line(fp) == EOF) {
  302.       PutStr("Unexpected EOF found\n");
  303.       IORTNCDE = -1;
  304.       AbortFlag = 1;
  305.       fclose(fp);
  306.       return(0);
  307.     }
  308.   } while (nocase_strcmp(response,"CITIES") != 0);
  309.     
  310.   num_cities = 0;
  311.   city_ptr = cities;
  312.   while(1) {
  313.     if (get_line(fp) == EOF) break;
  314.     if (nocase_strcmp(response,"DISTANCES") == 0) break;
  315. #ifdef THINK_C
  316.     sscanf(response,"   %s  %Lf %Lf",
  317.      city_ptr->name,
  318.      &city_ptr->xloc,
  319.      &city_ptr->yloc);
  320. #else
  321.     sscanf(response,"   %s  %lf %lf",
  322.      city_ptr->name,
  323.      &city_ptr->xloc,
  324.      &city_ptr->yloc);
  325. #endif
  326.     city_ptr++;
  327.     num_cities++;
  328.   }
  329.  
  330.   fclose(fp);
  331.  
  332.   if ( num_cities * num_cities != noutp ) {
  333.     PutStr("Mismatch between city file and network\n");
  334.     IORTNCDE = -1;
  335.     AbortFlag = 1;
  336.     fclose(fp);
  337.     return(0);
  338.   }
  339.  
  340.   /* Calculate city coordinate extremes */
  341.   city_ptr = cities;
  342.   xlow = xhigh = city_ptr->xloc;
  343.   ylow = yhigh = city_ptr->yloc;
  344.   for ( wx = 1; wx < num_cities; wx++ ) {
  345.     city_ptr++;
  346.     if (xlow > city_ptr->xloc ) xlow = city_ptr->xloc;
  347.     if (xhigh < city_ptr->xloc ) xhigh = city_ptr->xloc;
  348.     if (ylow > city_ptr->yloc ) ylow = city_ptr->yloc;
  349.     if (yhigh < city_ptr->yloc ) yhigh = city_ptr->yloc;
  350.   }
  351.   diff = (xhigh - xlow) - (yhigh - ylow);
  352.  
  353.   if ( diff > 0.0 ) {
  354.     ylow = ylow - diff/2.0;
  355.     yhigh = yhigh + diff/2.0;
  356.   } else {
  357.     xlow = xlow + diff/2.0;
  358.     xhigh = xhigh - diff/2.0;
  359.   }
  360.   scl = xhigh - xlow;
  361.   if ( scl < 0.01 ) scl = 0.01;
  362.   
  363.   /* Get screen parameters */
  364.   ug_gparms( &xsize, &ysize, &ncolor, &chrx, &chry);
  365.   
  366.   /* Assume screen aspect ratio of 4:3 */
  367.   scrn_ratio = (4.0 * ysize) / ( 3.0 * xsize );
  368.   height = width * scrn_ratio;
  369.   ymargin = xmargin * scrn_ratio;
  370.   xstrt = (xsize - width) / 2 - xmargin;
  371.   ystrt = (ysize - height) / 2 - ymargin;
  372.  
  373.   /* Calculate city screen coordinates */
  374.   for ( wx=0, city_ptr=cities; wx < num_cities; wx++, city_ptr++ ) {
  375.     city_pos[wx][X] =
  376.       xmargin + (int) ( (city_ptr->xloc - xlow) * width / scl );
  377.     city_pos[wx][Y] =
  378.       ymargin + (int) ( (city_ptr->yloc - ylow) * height / scl );
  379.   }
  380.  
  381.   if ( ncolor < 8 ) {
  382.     gm_intcolor = 1;
  383.     gm_txtcolor = 0;
  384.     gm_outcolor = 0;
  385.   } else {
  386.     gm_intcolor = 7;
  387.     gm_txtcolor = 4;
  388.     gm_outcolor = 0;
  389.   }
  390.  
  391.   InitGMenu( &TspMenu, chrx, chry );
  392.   xmwdw = (xsize - TspMenu.x1) / 2;
  393.   ymwdw = ystrt / 2;
  394.   TspMenu.x0 = 0; /* Menu position relative to window */
  395.   TspMenu.y0 = 0;
  396.   
  397.   /* Unlock all menu items */
  398.   for (gmip = TspMenu.item, wx = 0; wx < TspMenu.num_items;
  399.          gmip++, wx++ )
  400.       gmip->flag &= ~GM_LOCKED;
  401.  
  402.         /* Other initializations */
  403.   sprintf( sp, "%3ld", (long)stepsize );
  404.   strncpy( &TspList[TSP_STEPSIZE].text[11], sp, 3 );
  405.   for ( wx = 0; wx < num_cities; wx++ ) city_stop[wx] = wx;
  406.   curr_iteration = prev_iteration = 0;
  407.  
  408.   /* define windows */
  409.   ug_window( 1, gm_intcolor, xstrt, ystrt,
  410.        xstrt + 2*xmargin + width - 1,
  411.        ystrt + 2*ymargin + height - 1 );
  412.   ug_boxf( 1, gm_outcolor, 0, 0, 0,
  413.      2*xmargin + width - 1,
  414.      2*ymargin + height - 1 );
  415.   ug_boxf( 1, gm_intcolor, 0, 3, 3,
  416.      2*xmargin + width - 4,
  417.      2*ymargin + height - 4 );
  418.  
  419.  
  420.   ug_window( TspMenu.key, gm_intcolor, xmwdw, ymwdw,
  421.        xmwdw + TspMenu.x1 + 2, ymwdw + TspMenu.y1 + 2);
  422.  
  423.   DispGMenu( &TspMenu );
  424.  
  425.   break;
  426.  
  427.     case RQ_LEARNIN:        /* read training input */
  428.   /* IODATA points to an empty array of IOCOUNT elements.  The
  429.      values placed in this array by the user will become the
  430.      inputs to the network for training purposes.
  431.   */
  432.   break;
  433.       
  434.     case RQ_READ:       /* read test data */
  435.   /* IODATA points to an empty array of IOCOUNT values.  The
  436.      user must fill in these values.  The elements of the
  437.      array will become the "sum" of the inputs to the input
  438.      layer of processing elements.
  439.   */ 
  440.   break;
  441.  
  442.     case RQ_WRSTEP:       /* write interim results */
  443.   /* each recall cycle for the Hopfield and BAM control strategies,
  444.      the intermediate output is made available to userio to test
  445.      for convergence or other desired states.  This option is
  446.      not used for other control strategies.
  447.   */
  448.   if (AbortFlag) {
  449.     IORTNCDE = -1;
  450.     return(0);
  451.   } 
  452.   curr_iteration++;
  453.   if( curr_iteration - prev_iteration == stepsize ) {
  454.     prev_iteration = curr_iteration;
  455.     sprintf( sp, "Iteration number %3ld\n", (long)curr_iteration );
  456.     PutStr( sp );
  457.  
  458.     disp_itin( gm_intcolor, gm_intcolor ); /* Erase itinerary */
  459.     /* Calculate city stops */
  460.     for ( wx = 0; wx < num_cities; wx++ ) {
  461.       maxout = -1.0;
  462.       for ( wy = wx, wz = 0; wz < num_cities; wz++, wy += num_cities) {
  463.         if ( IODATA[wy] >= maxout ) {
  464.     maxout = IODATA[wy];
  465.     city_stop[wx] = wz;
  466.         }
  467.       }
  468.     }
  469.     disp_itin( gm_txtcolor, gm_txtcolor );
  470.     sprintf( sp, "Length of itinerary = %f\n", calc_itin( ) );
  471.     PutStr( sp );
  472.  
  473.     for ( ; ; ) 
  474.       {
  475.         while ( (gmip=LookGMenu(&TspMenu,&button )) == (GMENU_ITEM *) 0) ;
  476.         switch( gmip->code ) 
  477.     {
  478.     case TSP_STEPSIZE:
  479. #ifdef MAC
  480.       if (button == MBUT_LEFT)
  481. #else
  482.       if (button == MBUT_RIGHT)
  483. #endif
  484.         stepsize++;
  485. #ifdef MAC
  486.       if (button == MBUT_RIGHT)
  487. #else
  488.       if (button == MBUT_LEFT) 
  489. #endif
  490.         {
  491.           stepsize--;
  492.           if ( stepsize < 1 ) stepsize = 1;
  493.         }
  494.       if (button == MBUT_LEFT || button == MBUT_RIGHT) 
  495.         {
  496.           sprintf( sp, "%3ld", (long)stepsize );
  497.           strncpy( &TspList[TSP_STEPSIZE].text[11], sp, 3 );
  498.           DispGItem( &TspMenu, gmip, gm_intcolor, gm_txtcolor );
  499.         }
  500.       break;
  501.     case TSP_NEXTSTEP:
  502.       if ( button == MBUT_RIGHT || button == MBUT_LEFT ) 
  503.         {
  504.           goto end_wr;
  505.         }
  506.       break;
  507.     case TSP_QUIT:
  508.       if ( button == MBUT_RIGHT || button == MBUT_LEFT ) 
  509.         {
  510.           IORTNCDE = -1;
  511.           goto end_wr;
  512.         }
  513.       break;
  514.     }
  515.       }
  516.         }
  517. end_wr:
  518.   break;
  519.  
  520.     case RQ_WRITE:        /* write out results */
  521.   /* IODATA points to an array of IOCOUNT "float" type values.
  522.      The values are the outputs of the top-most layer of the
  523.      network.
  524.   */
  525.  
  526.   if (AbortFlag) {
  527.     IORTNCDE = -1;
  528.     return(0);
  529.   } 
  530.     /* Calculate final itinerary */
  531.     for ( wx = 0; wx < num_cities; wx++ ) {
  532.       maxout = -1.0;
  533.       for ( wy = wx, wz = 0; wz < num_cities; wz++, wy += num_cities) {
  534.         if ( IODATA[wy] >= maxout ) {
  535.     maxout = IODATA[wy];
  536.     city_stop[wx] = wz;
  537.         }
  538.       }
  539.     }
  540.     disp_itin( gm_txtcolor, gm_txtcolor );
  541.     PutStr("Final Itinerary\n");
  542.     TspList[TSP_STEPSIZE].flag |= GM_LOCKED;
  543.     TspList[TSP_NEXTSTEP].flag |= GM_LOCKED;
  544.   
  545.     for ( ; ; ) {
  546.       while ( (gmip=LookGMenu(&TspMenu,&button )) == (GMENU_ITEM *) 0) ;
  547.         if ( button == MBUT_RIGHT || button == MBUT_LEFT ) {
  548.           IORTNCDE = -1;
  549.           goto end_write;
  550.         }
  551.     }
  552. end_write:
  553.   break;
  554.  
  555.  
  556.     case RQ_TERM:       /* terminate interface */
  557.   /* close any files which may be open.  Deallocate any memory
  558.      which was allocated.  (This is VERY VERY important.  If
  559.      allocated memory is NOT released, dos memory will become
  560.      fragmented and it will become necessary to reboot.
  561.   */
  562.  
  563.   PutStr( "bye bye" );
  564.   break;
  565.     }
  566.  
  567. }
  568.